package com.iotek.ssm.service.impl;

import com.iotek.ssm.mapper.*;
import com.iotek.ssm.pojo.*;
import com.iotek.ssm.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service("managerService")
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ApplySheetMapper applySheetMapper;

    @Autowired
    private EntryTableMapper entryTableMapper;

    @Autowired
    private EmployeeStatusMapper employeeStatusMapper;

    @Autowired
    private RewardPunishmentMapper rewardPunishmentMapper;

    @Autowired
    private PerformanceMapper performanceMapper;

    @Autowired
    private AttendanceMapper attendanceMapper;

    public Manager findByName(String name) throws Exception {
        return managerMapper.findByName(name);
    }

    public void addEmployee(User user, ApplySheet applySheet, EntryTable entryTable, EmployeeStatus employeeStatus) throws Exception {
        user.setEmploy(1);
        applySheet.setIsEmploy(1);
        applySheet.setIsInterview(1);
        entryTable.setEntryDate(new Date());
        employeeStatus.setChangeDate(new Date());
        employeeStatus.setStatus(1);
        userMapper.updateUser(user);       //更新用户为入职状态
        applySheetMapper.updateApplyInformation(applySheet);   //更新应聘表相关信息
        entryTableMapper.addEntryInformation(entryTable);   //添加入职表信息
        employeeStatusMapper.addStatus(employeeStatus);
    }

    public void notEmploy(Integer userId, Integer applyId) throws Exception {
        User user = new User();
        user.setEmploy(-1);         //未被录用
        user.setId(userId);
        ApplySheet applySheet = new ApplySheet();
        applySheet.setId(applyId);
        applySheet.setIsEmploy(-1);      //未被录用
        applySheet.setIsInterview(1);
        userMapper.updateUser(user);
        applySheetMapper.updateApplyInformation(applySheet);
    }

    public EntryTable getEmployee(CustomQueryVo customQueryVo) throws Exception {
        return entryTableMapper.findEntryInformation(customQueryVo);
    }

    public void addRP(RewardPunishment rewardPunishment) throws Exception {
        rewardPunishmentMapper.addReward(rewardPunishment);
    }

    public void expelEmployee(Integer uId, Integer rId) throws Exception {
        User user = new User();
        user.setId(uId);
        user.setEmploy(-1);
        userMapper.updateUser(user);
        EmployeeStatus employeeStatus = new EmployeeStatus();
        employeeStatus.setChangeDate(new Date());
        employeeStatus.setStatus(-1);
        Resume resume = new Resume();
        resume.setId(rId);
        employeeStatus.setResume(resume);
        employeeStatusMapper.updateStatus(employeeStatus);
    }

    public List<Resume> findResumeList(Integer pId) throws Exception {
        return userMapper.findResumeList(pId);
    }

    public List<EntryTable> findEntryList(CustomQueryVo customQueryVo) throws Exception {
        return entryTableMapper.findEntryList(customQueryVo);
    }

    public List<SalarySheet> getSalaryList(CustomQueryVo customQueryVo) throws Exception {
        List<SalarySheet> salaryList = new ArrayList<SalarySheet>();
//        List<EntryTable> entryList = entryTableMapper.findEntryListBefore(customQueryVo);
        customQueryVo.setDate(new Date());
        List<EntryTable> entryList = entryTableMapper.findEntryList(customQueryVo);   //获取本月1号之前入职的员工简介
        if (entryList != null) {
            for (EntryTable et : entryList) {
                System.out.println("**********************" + et);
                SalarySheet salarySheet = new SalarySheet();
                Float regularPay = et.getPositionLevel().getRegularPay();      //获取基本工资
                Float socialSecurity = et.getPositionLevel().getSocialSecurity();   //获取社保费
                customQueryVo.setResumeId(et.getResume().getId());    //获取resume编号
                Performance performance = performanceMapper.findPerformanceDetail(customQueryVo);    //获取上月绩效
                Float totalExtraTimes = attendanceMapper.getTotalExtraTimes(customQueryVo);    //获取上月总加班时间
                Float totalExtraMoney = totalExtraTimes * 12;                              //计算获取上月总加班费
                Integer totalRewardMoney = rewardPunishmentMapper.getTotalRewardMoney(customQueryVo);    //获取上月总的奖惩金
                if (totalRewardMoney == null)
                    totalRewardMoney = 0;

                Float finalPay = 0f;
                if (performance != null) {
                    finalPay = regularPay - socialSecurity + totalExtraMoney + totalRewardMoney + performance.getPerformance();
                    salarySheet.setPerformance(performance);
                }
                salarySheet.setEntryTable(et);
                salarySheet.setExtraWorkPay(totalExtraMoney);
                salarySheet.setRewardPunishment(totalRewardMoney);
                salarySheet.setFinalPay(finalPay);
                salaryList.add(salarySheet);
            }
        }
        return salaryList;
    }
}
